package com.yx.controller;

import com.yx.anno.LogOperation;
import com.yx.pojo.Dept;
import com.yx.pojo.Result;
import com.yx.service.DeptService;
import com.yx.utils.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j//日志信息
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    /***
     * 查询部门信息
     * @return
     */
    @GetMapping
    public Result list(){
        log.info("查询部门全部信息");
        List<Dept> deptList = deptService.list();
        return ResultUtils.success(deptList);
    }
    /***
     * 删除部门
     */
    @LogOperation
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) throws Exception {//路径参数
        log.info("根据id删除部门：{}",id);
        deptService.delete(id);
        return ResultUtils.success();
    }

    /**
     * 新增部门
     */
    @LogOperation
    @PostMapping()
    public Result add(@RequestBody Dept dept){
        log.info("新增部门为：{}",dept.getName());
        deptService.add(dept);
        return ResultUtils.success();
    }

    /**
     * 根据id查询
     * @return
     */
    @GetMapping("/{id}")
    public Result selectByid(@PathVariable Integer id){
        log.info("根据id查询：{}",id);
        Dept dept = deptService.selectById(id);
        return ResultUtils.success(dept);
    }

    /**
     * 修改部门信息
     * @return
     */
    @LogOperation
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改后的部门名字为：{}",dept.getName());
        deptService.update(dept);
        return ResultUtils.success();
    }
}
